package com.hehui.partimejob;

import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.hehui.partimejob.network.MyNetwork;
import com.hehui.partimejob.network.OnNetworkListener;
import com.hehui.partimejob.service.model.UserModel;
import com.hehui.partimejob.service.vo.ServiceDataVo;
import com.hehui.partimejob.uitls.MyUtil;
import com.hehui.partimejob.view.InputView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LoginActivity extends BaseActivity {
    private LinearLayout loginContainer;
    private TextView btn;
    private TextView register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getNavView().setTitle(R.string.login);
        InputView inputView = new InputView();
        ArrayList<InputView.InputData> inputData = new ArrayList<>();
        inputData.add(new InputView.InputData().setId(View.generateViewId()).setIcon(R.drawable.username).setHint(getString(R.string.username_hint)));
        inputData.add(new InputView.InputData().setId(View.generateViewId()).setIcon(R.drawable.pwd).setHint(getString(R.string.password_hint)));
        List<InputView.Item> items = inputView.addInputView(this, inputData);
        items.get(1).getEditText().setTransformationMethod(PasswordTransformationMethod.getInstance());
        loginContainer.addView(inputView.getmContainer(), 1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = items.get(0).getValue();
                String password = items.get(1).getValue();
                if(!MyUtil.checkHasValue(A,
                        username,
                        password)){
                    return;
                }
                HashMap<String, Object> body = new HashMap<>();
                body.put("username",username);
                body.put("password",password);
                MyNetwork.getInstance(A).post("/user/login", body, new OnNetworkListener<UserModel>() {
                    @Override
                    public void handleData(ServiceDataVo<UserModel> data) {
                        super.handleData(data);
                      UserModel data1 = data.getData();
                        if(data1==null){
                            Toast.makeText(A, "登录失败！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        startActivity(MainActivity.class);
                    }
                });
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(RegisterActivity.class);
            }
        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {

        loginContainer = (LinearLayout) findViewById(R.id.loginContainer);
        btn = (TextView) findViewById(R.id.btn);
        register = (TextView) findViewById(R.id.register);

    }
}